Code
# Define a constructor
setClass("rational",
slots = c(a = "ANY",
b = "ANY"))
rational <- function(x1, x2) {
return(new("rational", a = as.numeric(x1), b = as.numeric(x2)))
}
# Define a validator
setValidity("rational", function(object){
newa <- as.numeric(object@a)
newb <- as.numeric(object@b)
if (is.na(newa)|is.na(newb)){
stop("Please input valid number")
}
if (newb == 0) {
stop("Denominator cannot be zero")
} else if (newa%%1 != 0 | newb%%1 != 0){
stop("Please input integer numerator or denominator")
}
return(TRUE)
})Class "rational" [in ".GlobalEnv"]
Slots:
Name: a b
Class: ANY ANY
Code
# Define the show method
##' @title print a `rational`
##' @param object a `rational` object
##' @return the numerator and denominator of the `rational` object
setMethod("show", "rational",
function(object) {
cat("Numerator:", object@a)
cat("\n")
cat("Denominator:", object@b)
return(invisible(object))
}
)
# Create simplify function
# First I will write a function to calculate greatest common divisor
##' @title report the greatest common divisor of two integers
##' @param a is the numerator
##' @param b is the denominator
##' @return the greatest common divisor
gcb <- function(a, b){
if(a == 0 | b == 0){
c <- NULL
return(c)
next()
}else if(a <= b){
smaller <- a
}else if(a > b){
smaller <- b
}
for (i in 1:smaller){
if(a%%i == 0 & b%%i == 0){
c <- i
}
}
return(c)
}
# Second I will create the simplify function
setGeneric("simplify",
function(object) {
standardGeneric("simplify")
})[1] "simplify"
Code
##' @title report simplest form a `rational` object
##' @param object a `rational` object
##' @return simplest form a `rational` object
setMethod("simplify", "rational",
function(object) {
c <- gcb(object@a, object@b)
if(length(c) != 0){
object@a <- object@a/c
object@b <- object@b/c
}
return(object)
})
# Create quotient function
setGeneric("quotient",
function(object, ...) {
standardGeneric("quotient")
})[1] "quotient"
Code
##' @title report quotient a `rational` object
##' @param object a `rational` object
##' @return quotient from a `rational` object
setMethod("quotient", "rational",
function(object, digits = 2, ...) {
newdig <- as.numeric(digits)
if (is.na(newdig)){
stop("Please input valid digits number")
}else if (newdig%%1 != 0){
stop("Please input valid digits number")
}else if (newdig%%1 == 0){
c <- object@a/object@b
print(invisible(c), digits = newdig)
}
})
# Define addition; substraction, multiplication and division function
##' @title `rational` addition arithmetic.
##' @param e1 A `rational` object
##' @param e2 A `rational` object
##' @return A `rational` object
setMethod("+", signature(e1 = "rational",
e2 = "rational"),
function(e1, e2) {
return(invisible(simplify(rational(e1@a*e2@b + e2@a*e1@b, e1@b*e2@b))))
})
##' @title `rational` subtraction arithmetic.
##' @param e1 A `rational` object
##' @param e2 A `rational` object
##' @return A `rational` object
setMethod("-", signature(e1 = "rational",
e2 = "rational"),
function(e1, e2) {
return(invisible(simplify(rational(e1@a*e2@b - e2@a*e1@b, e1@b*e2@b))))
})
##' @title `rational` multiplication arithmetic.
##' @param e1 A `rational` object
##' @param e2 A `rational` object
##' @return A `rational` object
setMethod("*", signature(e1 = "rational",
e2 = "rational"),
function(e1, e2) {
x1 <- simplify(e1)
x2 <- simplify(e2)
return(invisible(simplify(rational(x1@a*x2@a , x1@b*x2@b))))
})
##' @title `rational` division arithmetic.
##' @param e1 A `rational` object
##' @param e2 A `rational` object
##' @return A `rational` object
setMethod("/", signature(e1 = "rational",
e2 = "rational"),
function(e1, e2) {
x1 <- simplify(e1)
x2 <- simplify(e2)
return(invisible(simplify(rational(x1@a*x2@b , x1@b*x2@a))))
})